package org.hong.monkey.deploy.client

import org.hong.monkey.rpc.RpcEnv
import org.hong.monkey.{MonkeyConf, Logging}
import org.hong.monkey.deploy.{ApplicationDescription, Command}
import org.hong.monkey.util.Utils

private[monkey] object TestClient {

  private class TestListener extends AppClientListener with Logging {
    def connected(id: String) {
      logInfo("Connected to master, got app ID " + id)
    }

    def disconnected() {
      logInfo("Disconnected from master")
      System.exit(0)
    }

    def dead(reason: String) {
      logInfo("Application died with error: " + reason)
      System.exit(0)
    }

    def executorAdded(id: String, workerId: String, hostPort: String, cores: Int, memory: Int) {}

    def executorRemoved(id: String, message: String, exitStatus: Option[Int]) {}
  }

  def main(args: Array[String]) {
    val url = args(0)
    val conf = new MonkeyConf
    val rpcEnv = RpcEnv.create("monkey", Utils.localHostName(), 0, conf)
    val executorClassname = TestExecutor.getClass.getCanonicalName.stripSuffix("$")
    val desc = new ApplicationDescription("TestClient", Some(1), 512,
      Command(executorClassname, Seq(), Map(), Seq(), Seq(), Seq()), "ignored")
    val listener = new TestListener
    val client = new AppClient(rpcEnv, Array(url), desc, listener, new MonkeyConf)
    client.start()
    rpcEnv.awaitTermination()
  }
}
